package com.weiwei.base.application;

import java.util.LinkedList;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.weiwei.base.common.CustomLog;
import com.weiwei.base.dataprovider.GlobalVariables;
import com.weiwei.base.service.VsCoreService;
import com.weiwei.lock.LockPatternUtils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.pgyersdk.crash.PgyCrashManager;

public class VsApplication extends Application {
    private static final String TAG = VsApplication.class.getSimpleName();

    public static Context mContext;

    private LinkedList<Activity> mActivityList = new LinkedList<Activity>();
    private static VsApplication mInstance;
    private LockPatternUtils mLockPatternUtils;
    @Override
    public void onCreate() {
        CustomLog.i(TAG, "onCreate()..");
        super.onCreate();
        mContext = getApplicationContext();
        CrashHandler.getInstance().init(mContext, android.os.Build.BRAND + ":" + Build.MODEL);
        Intent serviceIntent = new Intent(this, VsCoreService.class);
        this.startService(serviceIntent);
        mLockPatternUtils = new LockPatternUtils(this);
        initImageLoader(getApplicationContext());
        PgyCrashManager.register(this);
    }


    /**
     * 单例模式中获取唯一的MyApplication实例
     *
     * @return
     */
    public static VsApplication getInstance() {
        if (null == mInstance) {
            mInstance = new VsApplication();
        }
        return mInstance;
    }

    /**
     * 获取Context.
     *
     * @return
     */
    public static Context getContext() {
        return mContext;
    }

    /**
     * 添加Activity到容器中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (mActivityList == null) {
            mActivityList = new LinkedList<Activity>();
        }
        mActivityList.add(activity);
    }

    /**
     * 取得当前打开的Activity个数
     */
    public int getActivitySize() {
        if (mActivityList == null) {
            return 0;
        }
        return mActivityList.size();
    }

    /**
     * 遍历所有Activity并finish
     */
    public void exit() {
        GlobalVariables.curIndicator = 0;
        if (mActivityList == null) {
            return;
        }
        for (Activity activity : mActivityList) {
            if (activity != null)
                activity.finish();

        }
        ImageLoader.getInstance().clearDiscCache();
        ImageLoader.getInstance().clearMemoryCache();
        mActivityList.clear();
        mActivityList = null;
    }


    private void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(50 * 1024 * 1024) // 50 Mb
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs() // Remove for release app
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

}
